package com.rtx.edi.platform.company.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rtx.edi.model.application.po.PlApplication;
import com.rtx.edi.model.application.po.PlApplicationEnvironment;
import com.rtx.edi.model.company.dto.PlCompanyDTO;
import com.rtx.edi.model.company.po.PlCompany;
import com.rtx.edi.platform.application.mapper.PlApplicationEnvironmentMapper;
import com.rtx.edi.platform.application.mapper.PlApplicationMapper;
import com.rtx.edi.platform.company.mapper.PlCompanyMapper;
import com.rtx.edi.platform.company.service.PlCompanyService;

/**
 * <p>
 * 公司信息表 服务实现类
 * </p>
 *
 * @author cafebabe
 * @since 2023-03-10
 */
@Service
public class PlCompanyServiceImpl extends ServiceImpl<PlCompanyMapper, PlCompany> implements PlCompanyService {

	@Autowired
	PlApplicationEnvironmentMapper plApplicationEnvironmentMapper;
	
	@Autowired
	PlApplicationMapper plApplicationMapper;
	
	@Autowired
	PlCompanyMapper plCompanyMapper;
	
	@Override
	public void selectPlCompanyList(Page<PlCompany> pageParam, PlCompany qcd) {
	       QueryWrapper<PlCompany> query = new QueryWrapper<>();
	        query.orderByAsc("company_no,company_status");
	        if(qcd != null){
	            //公司编号、公司名称、公司类型、公司状态
	        	if(StringUtils.hasText(qcd.getCompanyNo())) {
	        		query.like("company_no", qcd.getCompanyNo());
	        	}
	        	if(StringUtils.hasText(qcd.getCompanyName())) {
	        		query.like("company_name", qcd.getCompanyName());
	        	}
	        	if(qcd.getCompanyType()!=null) {
	        		query.eq("company_type", qcd.getCompanyType());
	        	}
	        	if(qcd.getCompanyStatus()!=null) {
	        		query.eq("company_status", qcd.getCompanyStatus());
	        	}
	        }
	        baseMapper.selectPage(pageParam,query);
	}

	public void deletePlApplicationByIds(List<PlCompanyDTO> list) {
		for(PlCompanyDTO dto:list) {
			QueryWrapper<PlApplication> app = new QueryWrapper<PlApplication>();
			app.eq("company_id", dto.getId());
			List<PlApplication> appList = plApplicationMapper.selectList(app);
			if(appList.size() > 0) {
				throw new RuntimeException("公司已创建应用-"+appList.stream().map(temp->temp.getApplicationName()).collect(Collectors.joining(","))+"-不能删除");
			}
			
			QueryWrapper<PlApplicationEnvironment> appEnvironment = new QueryWrapper<PlApplicationEnvironment>();
			appEnvironment.eq("company_id", dto.getId());
			List<PlApplicationEnvironment> environmentList = plApplicationEnvironmentMapper.selectList(appEnvironment);
			if(appList.size() > 0) {
				throw new RuntimeException("公司已创建应用环境-"+environmentList.stream().map(temp->temp.getEnvironmentName()).collect(Collectors.joining(","))+"-不能删除");
			}
			plCompanyMapper.updIsDelteById("1", dto.getId()+"");
		}
	}

	public List<PlCompany> selectPlCompanyList() {
		return baseMapper.selectList(new QueryWrapper<>());
	}
}
